//
//  ZYHTTPResponseSerializer.m
//  ZYWebImage
//
//  Created by wzp on 2025/4/7.
//

#import "ZYHTTPResponseSerializer.h"




@implementation ZYHTTPResponseSerializer
+ (instancetype)serializer
{
    return [[self alloc] init];
}
- (instancetype)init
{
    if (self = [super init]) {
        self.acceptableStatusCodes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 100)];
        self.acceptableContentTypes = nil;
    }
    return self;
}

- (BOOL)validateResponse:(NSHTTPURLResponse *)response
                    data:(NSData *)data
                   error:(NSError * _Nullable __autoreleasing *)error
{
    /*
     验证response：
        1、验证数据是否可以接受的类型
        2、验证状态码是否成功状态码
     */
    BOOL isValid = true;
    NSError *validationError = nil;
    
    if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
        if (self.acceptableContentTypes && ![self.acceptableContentTypes containsObject:[response MIMEType]] && !([response MIMEType] == nil && [data length] == 0)) {
            
            if ([data length] > 0 && [response URL]) {
                NSMutableDictionary *mutableUserInfo = [@{
                                                          NSLocalizedDescriptionKey: [NSString stringWithFormat:NSLocalizedStringFromTable(@"Request failed: unacceptable content-type: %@", @"AFNetworking", nil), [response MIMEType]],
                                                          NSURLErrorFailingURLErrorKey:[response URL],
                                                          ZYNetworkingOperationFailingURLResponseErrorKey: response,
                                                        } mutableCopy];
                if (data) {
                    mutableUserInfo[ZYNetworkingOperationFailingURLResponseDataErrorKey] = data;
                }

                validationError = [NSError
                                   errorWithDomain:ZYURLResponseSerializationErrorDomain
                                   code:NSURLErrorCannotDecodeContentData
                                   userInfo:mutableUserInfo];
            }

            isValid = NO;
        }
        
        if (!validationError && self.acceptableStatusCodes && [self.acceptableStatusCodes containsIndex:response.statusCode] && [response URL]) {
            NSMutableDictionary *mutableUserInfo = [@{
                                               NSLocalizedDescriptionKey: [NSString stringWithFormat:NSLocalizedStringFromTable(@"Request failed: %@ (%ld)", @"AFNetworking", nil), [NSHTTPURLResponse localizedStringForStatusCode:response.statusCode], (long)response.statusCode],
                                               NSURLErrorFailingURLErrorKey:[response URL],
                                               ZYNetworkingOperationFailingURLResponseErrorKey: response,
                                       } mutableCopy];

            if (data) {
                mutableUserInfo[ZYNetworkingOperationFailingURLResponseDataErrorKey] = data;
            }

            validationError = [NSError errorWithDomain:ZYURLResponseSerializationErrorDomain code:NSURLErrorBadServerResponse userInfo:mutableUserInfo];

            isValid = NO;
        }
    }
    
    if (validationError && !isValid) {
        *error = validationError;
    }
    return isValid;
}

- (id)responseObjectForResponse:(NSURLResponse *)response
                           data:(NSData *)data
                          error:(NSError * _Nullable __autoreleasing *)error
{
    [self validateResponse:(NSHTTPURLResponse *)response
                      data:data
                     error:error];
    return data;
}
@end
